/*
 * Copyright 2011-2012 Redia A/S
 * 
 * This file is part of Indhold+ for Android.
 *
 *  Redia Library App for Android is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  Redia Library App for Android is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License
 *  along with Redia Library App for Android.  If not, see <http://www.gnu.org/licenses/>.
 */
package dk.redia.digiform.xmlrpc;

import java.util.Map;

import dk.redia.lib.xmlrpc.XmlRpcClientCallTask;
import dk.redia.lib.xmlrpc.XmlRpcClientResultListener;


public class DigiformXmlRpcClient {
	
	private static DigiformXmlRpcClient instance;
	private XmlRpcClientCallTask task;
	
	public static DigiformXmlRpcClient getInstance() {
		if (instance==null) {
			instance = new DigiformXmlRpcClient();
		}
		return instance;
	}
	
	/**
	 * Will cancel the last task started with XmlRpcClientCallTask
	 * @param mayInterruptIfRunning 
	 */
	public void cancelLast(boolean mayInterruptIfRunning){
		task.cancel(mayInterruptIfRunning);
	}
	
	public XmlRpcClientCallTask getObject(String resourceId, Map<String, Boolean> returnedFields, XmlRpcClientResultListener l) {
    	DigiformXmlRpcClientCall c = new DigiformXmlRpcClientCall();
    	c.resultListener = l;
    	c.methodName = "getObject";
    	c.params = new Object[] { resourceId, returnedFields };
    	task = new XmlRpcClientCallTask();
    	task.executeManaged(c);
    	return task;
	}
	public XmlRpcClientCallTask getObjectWithFaust(String faustId, Map<String, Boolean> returnedFields, XmlRpcClientResultListener l) {
    	DigiformXmlRpcClientCall c = new DigiformXmlRpcClientCall();
    	c.resultListener = l;
    	c.methodName = "getObjectWithFaust";
    	c.params = new Object[] { faustId, returnedFields };
    	task = new XmlRpcClientCallTask();
    	task.executeManaged(c);
    	return task;
	}
	
	public XmlRpcClientCallTask emailObject(String faustId, String recipient, XmlRpcClientResultListener l) {
    	DigiformXmlRpcClientCall c = new DigiformXmlRpcClientCall();
    	c.resultListener = l;
    	c.methodName = "emailObject";
    	c.params = new Object[] { faustId, recipient };
    	task = new XmlRpcClientCallTask();
    	task.executeManaged(c);
    	return task;
	}
	
	public XmlRpcClientCallTask emailEbook(String faustId, String recipient, XmlRpcClientResultListener l) {
    	DigiformXmlRpcClientCall c = new DigiformXmlRpcClientCall();
    	c.resultListener = l;
    	c.methodName = "emailEbook";
    	c.params = new Object[] { faustId, recipient };
    	task = new XmlRpcClientCallTask();
    	task.executeManaged(c);
    	return task;		
	}
	
	public XmlRpcClientCallTask emailAudioBook(String faustId, String recipient, XmlRpcClientResultListener l) {
    	DigiformXmlRpcClientCall c = new DigiformXmlRpcClientCall();
    	c.resultListener = l;
    	c.methodName = "emailAudioBook";
    	c.params = new Object[] { faustId, recipient };
    	task = new XmlRpcClientCallTask();
    	task.executeManaged(c);
    	return task;		
	}
	
}
